package com.tjetc.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.CategoryMapper;
import com.tjetc.dao.DishMapper;
import com.tjetc.entity.Dish;
import com.tjetc.entity.DishRequest;
import com.tjetc.service.DishService;
import com.tjetc.service.MerchantDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

//商家管理菜品的业务类
@Service
public class MerchantDishImpl implements MerchantDishService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishService dishService;


    //添加菜品
    @Transactional
    @Override
    public JsonResult addDish(Dish dish,Integer merchantId) {
        dish.setMerchantId(merchantId);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        return new JsonResult(0,"新增菜品成功",dishMapper.insert(dish));

    }


    //获得商家对应的所有菜品
    @Override
    public JsonResult getMerchantDish(String pageNum, String pageSize, Integer merchantId) {
        PageInfo pageInfo = dishService.selectDetailPageByMerchantId(merchantId, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        return new JsonResult<>(0, "获取菜品列表成功", pageInfo);
    }


    //获得订单菜品详情
    @Override
    public JsonResult getDishDetailById(String id) {
        Dish dish = dishMapper.selectDetailByDishId(Integer.parseInt(id));
        return new JsonResult(0, "获取成功", dish);
    }
    //更新菜品信息
    @Transactional
    @Override
    public JsonResult updateDish(Dish dish, Integer merchantId) {
        Dish dish1=dishService.selectById(dish.getId());
        dish.setMerchantId(merchantId);
        dish.setCreateTime(dish1.getCreateTime());
        dish.setUpdateTime(LocalDateTime.now());
        return new JsonResult(0,"插入成功",dishMapper.updateDishDetail(dish));
    }

    public JsonResult test(Dish dish) {
        int id = dishMapper.updateDishDetail(dish);
        return new JsonResult(0, "测试完毕，请到数据库查看结果", null);
    }
    //更改菜品状态
    @Override
    public JsonResult changeStatus(Integer id, Integer status) {
        return new JsonResult(0, "更改成功", dishMapper.changeStatusById(id, status));
    }
    //获得菜品审核列表
    @Override
    public JsonResult getMerchantExamine(Integer pageNum, Integer pageSize, Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dish> list = dishMapper.selectExamine(id);
        PageInfo pageInfo = new PageInfo(list);
        return new JsonResult<>(0, "获取审核列表成功", pageInfo);
    }

    @Override
    public Integer getNumberOfDish(Integer merchantId) {
        return dishMapper.countDishByMerchantId(merchantId);
    }

    @Override
    public Integer countDishWith(DishRequest dishRequest) {
        HashMap map=new HashMap<>();
        map.put("merchantId",dishRequest.getMerchantId());
        if(dishRequest.getLevel()!=null)map.put("level",dishRequest.getLevel());
        if(dishRequest.getName()!=null)map.put("name",dishRequest.getName());
        if(dishRequest.getBeginInventory()!=null)map.put("beginInventory",dishRequest.getBeginInventory());
        if(dishRequest.getEndInventory()!=null)map.put("endInventory",dishRequest.getEndInventory());
        if(dishRequest.getCategoryId()!=null)map.put("categoryId",dishRequest.getCategoryId());
        if(dishRequest.getBeginPrice()!=null)map.put("beginPrice",dishRequest.getBeginPrice());
        if(dishRequest.getEndPrice()!=null)map.put("endPrice",dishRequest.getEndPrice());
        if(dishRequest.getStatus()!=null)map.put("status",dishRequest.getStatus());
        return dishMapper.countDishWith(map);
    }

    @Override
    public JsonResult getDishWith(DishRequest dishRequest) {
        HashMap map=new HashMap<>();
        map.put("merchantId",dishRequest.getMerchantId());
        if(dishRequest.getLevel()!=null)map.put("level",dishRequest.getLevel());
        if(dishRequest.getName()!=null)map.put("name",dishRequest.getName());
        if(dishRequest.getBeginInventory()!=null)map.put("beginInventory",dishRequest.getBeginInventory());
        if(dishRequest.getEndInventory()!=null)map.put("endInventory",dishRequest.getEndInventory());
        if(dishRequest.getCategoryId()!=null)map.put("categoryId",dishRequest.getCategoryId());
        if(dishRequest.getBeginPrice()!=null)map.put("beginPrice",dishRequest.getBeginPrice());
        if(dishRequest.getEndPrice()!=null)map.put("endPrice",dishRequest.getEndPrice());
        if(dishRequest.getStatus()!=null)map.put("status",dishRequest.getStatus());
        PageHelper.startPage(dishRequest.getPageNum(),dishRequest.getPageSize());
        PageInfo pageInfo=new PageInfo(dishMapper.selectDishWith(map));
        return new JsonResult(0,"查询成功",pageInfo);
    }


}
